package limiter

import (
	"context"
	"github.com/redis/go-redis/v9"
	"time"
)

type LimiterType int

const (
	LimiterTypeLocal LimiterType = iota
	LimiterTypeRedis
)

// NewLimiter 根据类型创建限流器
func NewLimiter(limiterType LimiterType, redisClient *redis.Client) RateLimiter {
	switch limiterType {
	case LimiterTypeRedis:
		return NewRedisLimiter(redisClient)
	default:
		return NewLocalLimiter()
	}
}

// RateLimiter 限流器通用接口
type RateLimiter interface {
	// Allow 检查是否允许通过
	// key: 限流key
	// limit: 时间窗口内允许的最大请求数
	// window: 时间窗口大小
	// 返回: 是否允许通过，错误信息
	Allow(ctx context.Context, key string, limit int, window time.Duration) (bool, error)
}
